<?php
/*
	本插件：$this->model('pai')->方法名();
	其他地方调用：model('jy_paimerch/pai')->方法名();
*/
namespace jy_paimerch;
defined('BY_JYA') or exit('error');
class pai extends \plugin{
	function create($order){
		if(!$this->setting['isopen'] && !$this->setting['isopen2']){
			return;
		}
		if(!$this->setting['isopen'] && $order['merchid']){
			return;
		}
		if(!$this->setting['isopen2'] && !$order['merchid']){
			return;
		}
		//file_put_contents(ROOT_D.'/1.txt',ijson_encode($order));
		$this->order=$order;
		$price=$this->order['price'];
		foreach($this->order['goods'] as $goods){
			if(!$goods['api_data']){
				$goods['api_data']=pdo_getcolumn('jy_weishop_goods',array('id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']),'api_data');
			}
			if(!is_array($goods['api_data'])){
				$goods['api_data']=json_decode($goods['api_data'],true);
			}
			if($goods['api_data']['nojy_paimerch']==1){
				$price=$price-$goods['total_price'];
			}
		}
		$price=$price>0?$price:0;
		$order['price']=$price;
		if($price>0){
			$this->commission_order=array(
				'uniacid'=>$_SESSION['uniacid'],
				'merchid'=>$order['merchid'],
				'mid'=>$this->order['mid'],
				'ordersn'=>$this->order['ordersn'],
				'price'=>$price,
				'orderprice'=>$this->order['price'],
				'jindu'=>0,
				'cantifee'=>0,
				'fee1'=>$this->setting['pai_fee1']*$price/100,
				'fee2'=>$this->setting['pai_fee2']*$price/100,
				'fee3'=>$this->setting['pai_fee3']*$price/100,
				'createtime'=>TIMESTAMP,
			);
		}
		$fromuser=pdo_getcolumn('core_members',array('id'=>$this->order['mid'],'uniacid'=>$_SESSION['uniacid']),'fromuser');
		$this->commission_order['fromuser']=$fromuser;
		//产生排号
		$this->up_paorder($order);	
		//直推加速
		if($fromuser){
			$this->pai_tiu($order,$fromuser);	
		}
		//复购加速
		$this->pai_fbuy($order);			
		//生成分销订单
		$indata=$this->commission_order;
		$this->insert('order',$indata);
		//商家让利
		$pai_feerate=$this->setting['pai_feerate'];
		if($order['merchid']){
			$merchsetting=pdo_getcolumn('jy_merch_user',array('id'=>$order['merchid'],'uniacid'=>$_SESSION['uniacid']),'setting');
			$merchsetting=json_decode($merchsetting,true);
			if($merchsetting['paifeenew']){
				$pai_feerate=$merchsetting['paifeenew'];
			}
		}
		$fee=$price*$pai_feerate/100;
		if($fee>0){
			$tablename=$order['tablename']?:'jy_weishop_order';
			$order['api_data']=$order['api_data']?:pdo_getcolumn($tablename,array('id'=>$order['id'],'uniacid'=>$_SESSION['uniacid']),'api_data');
			if(!is_array($order['api_data'])){
				$order['api_data']=json_decode($order['api_data'],true);
			}
			$order['api_data']['fanli_fee']=$fee;
			pdo_update($tablename,array('api_data'=>ijson_encode($order['api_data'])),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['id']));
		}
		$this->addfeelog($indata,$fee,0);
		return true;
	}
	/*
		产生排号;
	*/
	function up_paorder($order=array()){
		$orders=pdo_getall('jy_paimerch_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>0),'','','id asc');
		$pai_jindus=array();
		$isjindu=0;
		foreach($this->setting['pai_jindus'] as $ov){
			$pai_jindus[$ov['num']]=$ov['jindu'];
			$isjindu +=$ov['jindu'];
		}
		$ordernum=count($orders)-count($this->setting['pai_jindus']);
		if($ordernum>0){
			$onejindu=(100-$isjindu)/$ordernum;
		}else{
			$onejindu=100-$isjindu;
		}
		$isfeechi=$this->setting['isfeechi'];
		if($isfeechi){
			$fee0all=pdo_getsum('jy_paimerch_feelog',array('uniacid'=>$_SESSION['uniacid'],'type'=>0),'fee');
			$cantifee=pdo_getsum('jy_paimerch_order',array('uniacid'=>$_SESSION['uniacid']),'cantifee');
			$lastfee=$fee0all-$cantifee;
			if($lastfee<=0){
				return '';
			}
		}
		$forderid=$order['id'];
		foreach($orders as $i=>$ov){
			$num=$i+1;
			$jindu=isset($pai_jindus[$num])?$pai_jindus[$num]:$onejindu;
			if(!$jindu || $jindu<=0){
				continue;
			}
			if($isfeechi){
				$jindufee=$jindu*$lastfee/100;
				if(!$jindufee || $jindufee<0.01){
					break;
				}
				if($jindufee>$ov['price']-$ov['cantifee']){
					$cantifee1=$ov['price']-$ov['cantifee'];
					$jindu=100;
				}else{
					$cantifee1=$jindufee;
					$jindu=$cantifee1*100/$ov['price'];
				}
			}else{
				$cantifee1=$jindu*$ov['price']/100;
			}
			if(!$cantifee1 || $cantifee1<0.01){
				continue;
			}
			if($ov['jindu']+$jindu>=100){
				$this->pai_succ($ov);
			}else{
				pdo_update('jy_paimerch_order',array('jindu +='=>$jindu,'cantifee +='=>$cantifee1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$ov['id']));
			}
			$this->addpailog($ov,$jindu,2,$num,$forderid);
		}
		return true;
	}
	/*
		直推加速;
	*/
	function pai_tiu($order=array(),$fromuser=''){
		$jindu=$this->setting['pai_tiu'];
		if(!$jindu || $jindu<=0){
			return true;
		}
		$forderid=$order['id'];
		if($jindu && $fromuser){
			$order=pdo_get('jy_paimerch_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$fromuser,'status'=>0));
			if($order['id']){
				$isfeechi=$this->setting['isfeechi'];
				if($isfeechi){
					$fee0all=pdo_getsum('jy_paimerch_feelog',array('uniacid'=>$_SESSION['uniacid'],'type'=>0),'fee');
					$cantifee=pdo_getsum('jy_paimerch_order',array('uniacid'=>$_SESSION['uniacid']),'cantifee');
					$lastfee=$fee0all-$cantifee;
					if($lastfee<=0){
						return '';
					}
					$jindufee=$jindu*$lastfee/100;
					if($jindufee>$order['price']-$order['cantifee']){
						$cantifee1=$order['price']-$order['cantifee'];
						$jindu=100;
					}else{
						$cantifee1=$jindufee;
						$jindu=$cantifee1*100/$order['price'];
					}
				}
				$cantifee=($order['jindu']+$jindu)*$order['price']/100;
				if($order['jindu']+$jindu>=100){
					$this->pai_succ($order);
				}else{
					pdo_update('jy_paimerch_order',array('jindu +='=>$jindu,'cantifee'=>$cantifee),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['id']));
				}
				$this->addpailog($order,$jindu,0,0,$forderid);
			}
		}
		return true;
	}
	/*
		复购加速;
	*/
	function pai_fbuy($order=array()){
		$jindu=$this->setting['pai_fbuy'];
		if(!$jindu || $jindu<=0){
			return true;
		}
		$forderid=$order['id'];
		if($jindu){
			$isbuy=pdo_count('jy_paimerch_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid'],'status'=>0));
			if(!$isbuy){
				return '';
			}
			$order=pdo_get('jy_paimerch_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid'],'status'=>0));
			if($order['id']){
				$isfeechi=$this->setting['isfeechi'];
				if($isfeechi){
					$fee0all=pdo_getsum('jy_paimerch_feelog',array('uniacid'=>$_SESSION['uniacid'],'type'=>0),'fee');
					$cantifee=pdo_getsum('jy_paimerch_order',array('uniacid'=>$_SESSION['uniacid']),'cantifee');
					$lastfee=$fee0all-$cantifee;
					if($lastfee<=0){
						return '';
					}
					$jindufee=$jindu*$lastfee/100;
					if($jindufee>$order['price']-$order['cantifee']){
						$cantifee1=$order['price']-$order['cantifee'];
						$jindu=100;
					}else{
						$cantifee1=$jindufee;
						$jindu=$cantifee1*100/$order['price'];
					}
				}
				if($order['jindu']+$jindu>=100){
					$this->pai_succ($order);
				}else{
					$cantifee=($order['jindu']+$jindu)*$order['price']/100;
					pdo_update('jy_paimerch_order',array('jindu +='=>$jindu,'cantifee'=>$cantifee),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['id']));
				}
				$this->addpailog($order,$jindu,1,0,$forderid);
			}
		}
		return true;
	}
	/*
		免单出局;
	*/
	function pai_succ($order=array()){
		pdo_update('jy_paimerch_order',array('jindu'=>100,'cantifee'=>$order['price'],'status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['id']));
		$lastprice=$order['price']-$order['istifee'];
		//交给客户提现
		//model('core/member')->member_credit($order['mid'],'credit2',$lastprice,true,'jy_paimerch','免单返余额');
		$this->addfeelog($order,$lastprice,1);
		$fee=$order['fee1'];
		if($fee){
			model('core/member')->member_credit($order['mid'],'credit1',$fee,true,'jy_paimerch','免单出局返积分');
		}
		$fee=$order['fee2'];
		if($fee){
			model('core/member')->member_credit($order['mid'],'credit2',$fee,true,'jy_paimerch','免单出局返余额');
			$this->addfeelog($order,$fee,3);
		}
		$fee=$order['fee3'];
		if($fee){
			model('core/member')->member_credit($order['mid'],'credit3',$fee,true,'jy_paimerch','免单出局返购物币');
		}
		return true;
	}
	/*
		退出;
	*/
	function tuifan($orderid=0){
		pdo_update('jy_paimerch_order',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		$order=pdo_get('jy_paimerch_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		$lastprice=$order['price']-$order['cantifee'];
		$tuifee1=$this->setting['pai_tuifee1']*$lastprice/100;
		$tuifee2=$this->setting['pai_tuifee2']*$lastprice/100;
		$tuifee3=$this->setting['pai_tuifee3']*$lastprice/100;
		if($tuifee1>0){
			model('core/member')->member_credit($order['mid'],'credit1',$tuifee1,true,'jy_paimerch','退出返积分');
		}
		if($tuifee2>0){
			model('core/member')->member_credit($order['mid'],'credit2',$tuifee2,true,'jy_paimerch','退出返余额');
		}
		if($tuifee3>0){
			model('core/member')->member_credit($order['mid'],'credit3',$tuifee3,true,'jy_paimerch','退出返购物币');
		}
		$this->addfeelog($order,$tuifee2,2);
		return true;
	}
	/*
		提现;
	*/
	function tixian($orderid=0){
		$order=pdo_get('jy_paimerch_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		$order['cantifee']=$order['jindu']*$order['price']/100;
		$order['lastcantifee']=$order['cantifee']-$order['istifee'];
		if($order['lastcantifee']>0){
			$ti_feerate=$this->setting['ti_feerate']?:0;
			$choutifee=price($ti_feerate*$order['lastcantifee']/100,'int');
			pdo_update('jy_paimerch_order',array('istifee +='=>$order['lastcantifee']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
			$fee2=$order['lastcantifee']-$choutifee;
			model('core/member')->member_credit($order['mid'],'credit2',$fee2,true,'jy_paimerch','免单提现');
		}
		return true;
	}
	public function order_complete($order){
		$this->update('pailog',array('status'=>1),array('status'=>0,'orderid'=>$order['id']));
	}
	public function addpailog($order,$jindu,$type=0,$pai=0,$forderid=0){
		if($jindu<=0){
			return '';
		}
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$order['mid'],
			'merchid'=>$order['merchid'],
			'orderid'=>$order['id'],
			'jindu'=>$jindu,
			'type'=>$type,
			'pai'=>$pai,
			'forderid'=>$forderid,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_paimerch_pailog',$insert);
		return true;
	}
	public function addfeelog($order,$fee,$type=0){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$order['mid'],
			'merchid'=>$order['merchid'],
			'ordersn'=>$order['ordersn'],
			'fee'=>$fee,
			'type'=>$type,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_paimerch_feelog',$insert);
		return true;
	}
}
?>